﻿using App_csharp;
using TestKeyboard.DriverStageHelper;
using static JMBox.Config.R;

namespace TestKeyboard.PressKey
{
    public class PressKeyByDD : IPressKey
    {

        public static CDD dd = new CDD();

        #region DD参数 常量定义
        public static int DD_ESC = 100;
        public static int DD_F1 = 101;
        public static int DD_F2 = 102;
        public static int DD_F3 = 103;
        public static int DD_F4 = 104;
        public static int DD_F5 = 105;
        public static int DD_F6 = 106;
        public static int DD_F7 = 107;
        public static int DD_F8 = 108;
        public static int DD_F9 = 109;
        public static int DD_F10 = 110;
        public static int DD_F11 = 111;
        public static int DD_F12 = 112;
        public static int DD_Tilde = 200;//波浪号
        public static int DD_Num1 = 201;
        public static int DD_Num2 = 202;
        public static int DD_Num3 = 203;
        public static int DD_Num4 = 204;
        public static int DD_Num5 = 205;
        public static int DD_Num6 = 206;
        public static int DD_Num7 = 207;
        public static int DD_Num8 = 208;
        public static int DD_Num9 = 209;
        public static int DD_Num0 = 210;
        public static int DD_Sub = 211;
        public static int DD_Equal = 212;
        public static int DD_BackSlash = 213;//反斜杠\
        public static int DD_BackSpace = 214;//退格键
        public static int DD_TAB = 300;
        public static int DD_Q = 301;
        public static int DD_W = 302;
        public static int DD_E = 303;
        public static int DD_R = 304;
        public static int DD_T = 305;
        public static int DD_Y = 306;
        public static int DD_U = 307;
        public static int DD_I = 308;
        public static int DD_O = 309;
        public static int DD_P = 310;
        public static int DD_LeftSquareBracket = 311;//左中括号[
        public static int DD_RightSquareBracket = 312;//右中括号]
        public static int DD_Enter = 313;
        public static int DD_CapLock = 400;
        public static int DD_A = 401;
        public static int DD_S = 402;
        public static int DD_D = 403;
        public static int DD_F = 404;
        public static int DD_G = 405;
        public static int DD_H = 406;
        public static int DD_J = 407;
        public static int DD_K = 408;
        public static int DD_L = 409;
        public static int DD_Semicolon = 410;//分号;
        public static int DD_SingleQuotationMark = 411;//单引号'
        public static int DD_LeftShift = 500;
        public static int DD_Z = 501;
        public static int DD_X = 502;
        public static int DD_C = 503;
        public static int DD_V = 504;
        public static int DD_B = 505;
        public static int DD_N = 506;
        public static int DD_M = 507;
        public static int DD_Comma = 508;//逗号;
        public static int DD_Dot = 509;//点.
        public static int DD_Slash = 510;// 斜杠/
        public static int DD_RightShift = 511;
        public static int DD_LeftCtrl = 600;
        public static int DD_LeftWin = 601;
        public static int DD_LeftAlt = 602;
        public static int DD_Space = 603;
        public static int DD_RightAlt = 604;
        public static int DD_RightWin = 605;
        public static int DD_RightCtrl = 607;
        public static int DD_PrintSceen = 700;
        public static int DD_Scroll = 701;
        public static int DD_Pause = 702;
        public static int DD_Insert = 703;
        public static int DD_Home = 704;
        public static int DD_PageUp = 705;
        public static int DD_Del = 706;
        public static int DD_End = 707;
        public static int DD_PageDown = 708;
        public static int DD_Up = 709;
        public static int DD_Left = 710;
        public static int DD_Down = 711;
        public static int DD_Right = 712;
        public static int DD_NumPad0 = 800;
        public static int DD_NumPad1 = 801;
        public static int DD_NumPad2 = 802;
        public static int DD_NumPad3 = 803;
        public static int DD_NumPad4 = 804;
        public static int DD_NumPad5 = 805;
        public static int DD_NumPad6 = 806;
        public static int DD_NumPad7 = 807;
        public static int DD_NumPad8 = 808;
        public static int DD_NumPad9 = 809;
        public static int DD_NumLock = 810;
        public static int DD_NumPadDivision = 811; //小键盘除/
        public static int DD_NumPadMultiplication = 812;//小键盘乘*
        public static int DD_NumPadSubtraction = 813;//小键盘减-
        public static int DD_NumPadAdd = 814;//小键盘加+
        public static int DD_NumPadEnter = 815;//小键盘Enter

        #endregion

        public static Dictionary<Keys, int> STATIC_MAP = new Dictionary<Keys, int>();

        public void InitDict()
        {
            // 添加映射关系
            STATIC_MAP.TryAdd(Keys.Escape, 100);       // Escape -> DD_ESC
            STATIC_MAP.TryAdd(Keys.F1, 101);           // F1 -> DD_F1
            STATIC_MAP.TryAdd(Keys.F2, 102);           // F2 -> DD_F2
            STATIC_MAP.TryAdd(Keys.F3, 103);           // F3 -> DD_F3
            STATIC_MAP.TryAdd(Keys.F4, 104);           // F4 -> DD_F4
            STATIC_MAP.TryAdd(Keys.F5, 105);           // F5 -> DD_F5
            STATIC_MAP.TryAdd(Keys.F6, 106);           // F6 -> DD_F6
            STATIC_MAP.TryAdd(Keys.F7, 107);           // F7 -> DD_F7
            STATIC_MAP.TryAdd(Keys.F8, 108);           // F8 -> DD_F8
            STATIC_MAP.TryAdd(Keys.F9, 109);           // F9 -> DD_F9
            STATIC_MAP.TryAdd(Keys.F10, 110);          // F10 -> DD_F10
            STATIC_MAP.TryAdd(Keys.F11, 111);          // F11 -> DD_F11
            STATIC_MAP.TryAdd(Keys.F12, 112);          // F12 -> DD_F12
            STATIC_MAP.TryAdd(Keys.Oemtilde, 200);     // Oemtilde -> DD_Tilde
            STATIC_MAP.TryAdd(Keys.D1, 201);           // D1 -> DD_Num1
            STATIC_MAP.TryAdd(Keys.D2, 202);           // D2 -> DD_Num2
            STATIC_MAP.TryAdd(Keys.D3, 203);           // D3 -> DD_Num3
            STATIC_MAP.TryAdd(Keys.D4, 204);           // D4 -> DD_Num4
            STATIC_MAP.TryAdd(Keys.D5, 205);           // D5 -> DD_Num5
            STATIC_MAP.TryAdd(Keys.D6, 206);           // D6 -> DD_Num6
            STATIC_MAP.TryAdd(Keys.D7, 207);           // D7 -> DD_Num7
            STATIC_MAP.TryAdd(Keys.D8, 208);           // D8 -> DD_Num8
            STATIC_MAP.TryAdd(Keys.D9, 209);           // D9 -> DD_Num9
            STATIC_MAP.TryAdd(Keys.D0, 210);           // D0 -> DD_Num0
            STATIC_MAP.TryAdd(Keys.OemMinus, 211);     // OemMinus -> DD_Sub
            STATIC_MAP.TryAdd(Keys.Oemplus, 212);      // Oemplus -> DD_Equal
            STATIC_MAP.TryAdd(Keys.OemPipe, 213);      // OemPipe -> DD_BackSlash
            STATIC_MAP.TryAdd(Keys.Back, 214);         // Back -> DD_BackSpace
            STATIC_MAP.TryAdd(Keys.Tab, 300);          // Tab -> DD_TAB
            STATIC_MAP.TryAdd(Keys.Q, 301);            // Q -> DD_Q
            STATIC_MAP.TryAdd(Keys.W, 302);            // W -> DD_W
            STATIC_MAP.TryAdd(Keys.E, 303);            // E -> DD_E
            STATIC_MAP.TryAdd(Keys.R, 304);            // R -> DD_R
            STATIC_MAP.TryAdd(Keys.T, 305);            // T -> DD_T
            STATIC_MAP.TryAdd(Keys.Y, 306);            // Y -> DD_Y
            STATIC_MAP.TryAdd(Keys.U, 307);            // U -> DD_U
            STATIC_MAP.TryAdd(Keys.I, 308);            // I -> DD_I
            STATIC_MAP.TryAdd(Keys.O, 309);            // O -> DD_O
            STATIC_MAP.TryAdd(Keys.P, 310);            // P -> DD_P
            STATIC_MAP.TryAdd(Keys.OemOpenBrackets, 311); // OemOpenBrackets -> DD_LeftSquareBracket
            STATIC_MAP.TryAdd(Keys.OemCloseBrackets, 312); // OemCloseBrackets -> DD_RightSquareBracket
            STATIC_MAP.TryAdd(Keys.CapsLock, 400);     // CapsLock -> DD_CapLock
            STATIC_MAP.TryAdd(Keys.A, 401);            // A -> DD_A
            STATIC_MAP.TryAdd(Keys.S, 402);            // S -> DD_S
            STATIC_MAP.TryAdd(Keys.D, 403);            // D -> DD_D
            STATIC_MAP.TryAdd(Keys.F, 404);            // F -> DD_F
            STATIC_MAP.TryAdd(Keys.G, 405);            // G -> DD_G
            STATIC_MAP.TryAdd(Keys.H, 406);            // H -> DD_H
            STATIC_MAP.TryAdd(Keys.J, 407);            // J -> DD_J
            STATIC_MAP.TryAdd(Keys.K, 408);            // K -> DD_K
            STATIC_MAP.TryAdd(Keys.L, 409);            // L -> DD_L
            STATIC_MAP.TryAdd(Keys.OemSemicolon, 410); // OemSemicolon -> DD_Semicolon
            STATIC_MAP.TryAdd(Keys.OemQuotes, 411);    // OemQuotes -> DD_SingleQuotationMark
            STATIC_MAP.TryAdd(Keys.LShiftKey, 500);    // LShiftKey -> DD_LeftShift
            STATIC_MAP.TryAdd(Keys.Z, 501);            // Z -> DD_Z
            STATIC_MAP.TryAdd(Keys.X, 502);            // X -> DD_X
            STATIC_MAP.TryAdd(Keys.C, 503);            // C -> DD_C
            STATIC_MAP.TryAdd(Keys.V, 504);            // V -> DD_V
            STATIC_MAP.TryAdd(Keys.B, 505);            // B -> DD_B
            STATIC_MAP.TryAdd(Keys.N, 506);            // N -> DD_N
            STATIC_MAP.TryAdd(Keys.M, 507);            // M -> DD_M
            STATIC_MAP.TryAdd(Keys.Oemcomma, 508);     // Oemcomma -> DD_Comma
            STATIC_MAP.TryAdd(Keys.OemPeriod, 509);    // OemPeriod -> DD_Dot
            STATIC_MAP.TryAdd(Keys.OemQuestion, 510);  // OemQuestion -> DD_Slash
            STATIC_MAP.TryAdd(Keys.RShiftKey, 511);    // RShiftKey -> DD_RightShift
            STATIC_MAP.TryAdd(Keys.LControlKey, 600);  // LControlKey -> DD_LeftCtrl
            STATIC_MAP.TryAdd(Keys.LWin, 601);         // LWin -> DD_LeftWin
            STATIC_MAP.TryAdd(Keys.LMenu, 602);        // LMenu -> DD_LeftAlt
            STATIC_MAP.TryAdd(Keys.Space, 603);        // Space -> DD_Space
            STATIC_MAP.TryAdd(Keys.RMenu, 604);        // RMenu -> DD_RightAlt
            STATIC_MAP.TryAdd(Keys.RWin, 605);         // RWin -> DD_RightWin
            STATIC_MAP.TryAdd(Keys.RControlKey, 607);  // RControlKey -> DD_RightCtrl
            STATIC_MAP.TryAdd(Keys.PrintScreen, 700);  // PrintScreen -> DD_PrintSceen
            STATIC_MAP.TryAdd(Keys.Scroll, 701);       // Scroll -> DD_Scroll
            STATIC_MAP.TryAdd(Keys.Pause, 702);        // Pause -> DD_Pause
            STATIC_MAP.TryAdd(Keys.Insert, 703);       // Insert -> DD_Insert
            STATIC_MAP.TryAdd(Keys.Home, 704);         // Home -> DD_Home
            STATIC_MAP.TryAdd(Keys.PageUp, 705);       // PageUp -> DD_PageUp
            STATIC_MAP.TryAdd(Keys.Delete, 706);       // Delete -> DD_Del
            STATIC_MAP.TryAdd(Keys.End, 707);          // End -> DD_End
            STATIC_MAP.TryAdd(Keys.PageDown, 708);     // PageDown -> DD_PageDown
            STATIC_MAP.TryAdd(Keys.Up, 709);           // Up -> DD_Up
            STATIC_MAP.TryAdd(Keys.Left, 710);         // Left -> DD_Left
            STATIC_MAP.TryAdd(Keys.Down, 711);         // Down -> DD_Down
            STATIC_MAP.TryAdd(Keys.Right, 712);        // Right -> DD_Right
            STATIC_MAP.TryAdd(Keys.NumPad0, 800);      // NumPad0 -> DD_NumPad0
            STATIC_MAP.TryAdd(Keys.NumPad1, 801);      // NumPad1 -> DD_NumPad1
            STATIC_MAP.TryAdd(Keys.NumPad2, 802);      // NumPad2 -> DD_NumPad2
            STATIC_MAP.TryAdd(Keys.NumPad3, 803);      // NumPad3 -> DD_NumPad3
            STATIC_MAP.TryAdd(Keys.NumPad4, 804);      // NumPad4 -> DD_NumPad4
            STATIC_MAP.TryAdd(Keys.NumPad5, 805);      // NumPad5 -> DD_NumPad5
            STATIC_MAP.TryAdd(Keys.NumPad6, 806);      // NumPad6 -> DD_NumPad6
            STATIC_MAP.TryAdd(Keys.NumPad7, 807);      // NumPad7 -> DD_NumPad7
            STATIC_MAP.TryAdd(Keys.NumPad8, 808);      // NumPad8 -> DD_NumPad8
            STATIC_MAP.TryAdd(Keys.NumPad9, 809);      // NumPad9 -> DD_NumPad9
            STATIC_MAP.TryAdd(Keys.NumLock, 810);      // NumLock -> DD_NumLock
            STATIC_MAP.TryAdd(Keys.Divide, 811);       // Divide -> DD_NumPadDivision
            STATIC_MAP.TryAdd(Keys.Multiply, 812);     // Multiply -> DD_NumPadMultiplication
            STATIC_MAP.TryAdd(Keys.Subtract, 813);     // Subtract -> DD_NumPadSubtraction
            STATIC_MAP.TryAdd(Keys.Add, 814);          // Add -> DD_NumPadAdd
            STATIC_MAP.TryAdd(Keys.Enter, 815);        // Enter -> DD_NumPadEnter
        }
        public bool Initialize(EnumWindowsType winType)
        {
            int ret = dd.Load(Contact.DDriverPath);
            if (ret != 1) { MessageBox.Show("Load Error"); return false; }


            ret = dd.btn(0); //DD Initialize
            if (ret != 1) { MessageBox.Show("Initialize Error"); return false; }
            InitDict();

            return true;
        }

        public void KeyDown(Keys ch)
        {
            dd.key(STATIC_MAP[ch], 1);
        }

        public void KeyPress(Keys key)
        {
            KeyDown(key);//按下
            Thread.Sleep(10);
            KeyUp(key);//松开
        }

        public void KeyUp(Keys ch)
        {
            dd.key(STATIC_MAP[ch], 2);
        }
    }
}
